package com.flying.pas.security.service;

import java.util.Date;
import java.util.List;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.annotation.Transaction;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.context.ServiceContext;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;
import com.flying.framework.security.Principal;
import com.flying.framework.service.AbstractService;
import com.flying.pas.security.model.Role;
import com.flying.pas.security.model.RoleUser;
import com.flying.pas.security.model.User;

@Service(value="RoleService", desc="")
public class RoleService extends AbstractService{
	private RoleUserService rus;
	private RolePermService rps;
	
	public void setRoleUserService(RoleUserService s) {
		this.rus = s;
	}
	
	public void setRolePermService(RolePermService s) {
		this.rps = s;
	}
        
	@MethodInfo("新增")
	@DaoCreate(entity=Role.ENTITY_NAME)
	public Role create(
			@Param(value=Role.NAME, required=true,	desc="角色名称") String name,
			@Param(value=Role.ROLE_CODE, required=true,	desc="角色代码") String code,
			@Param(value=Role.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=Role.CREATOR, required=false,	desc="创建人") String creator,
			@Param(value=Role.CREATE_TIME, required=false,	desc="创建时间") Date create_time
		) throws Exception {
		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(Role.class, 
				Role.CREATOR, p == null?null: p.getName(),
				Role.CREATE_TIME, new Date());
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=Role.ENTITY_NAME)
	public ActionResult update(
			@Param(value=Role.ROLE_ID, required=true,	desc="角色ID") Long role_id,
			@Param(value=Role.NAME, required=true,	desc="角色名称") String name,
			@Param(value=Role.ROLE_CODE, required=true,	desc="角色代码") String code,
			@Param(value=Role.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=Role.UPDATE_TIME, required=false,	desc="修改时间") Date update_time,
			@Param(value=Role.UPDATOR, required=false,	desc="修改人") String updator
		) throws Exception {
		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(ActionResult.class, 
				Role.UPDATOR, p == null?null: p.getName(),
				Role.UPDATE_TIME, new Date());
	}

	@MethodInfo("删除")
	@Transaction
	@DaoRemove(entity=Role.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=Role.ROLE_ID, required=true,	desc="角色ID") Long role_id
		) throws Exception {
		this.rus.removeByRole(role_id);
		this.rps.removeByRole(role_id);
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=Role.ENTITY_NAME, pageable=true, modelClass=Role.class)
	public QueryResult<Role> findAll(
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) throws Exception {
		return null;
	}

	@MethodInfo("搜索")
	public QueryResult<Role> findAll2() throws Exception {
		return this.findAll(0, 100);
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=Role.ENTITY_NAME, pageable=true, modelClass=Role.class)
	public QueryResult<Role> findByIds(
			@Param(value=Role.ROLE_ID, required=true,	desc="角色ID", tag="IN") Long[] role_ids) throws Exception {
		return null;
	}

	@SuppressWarnings("unchecked")
	@MethodInfo("搜索")
	public QueryResult<Role> findByUserId(
			@Param(value=User.USER_ID, required=true,	desc="用户ID") Long user_id) throws Exception {
		List<RoleUser> list = this.rus.findByUserId(user_id).getRows();
		Long[] role_ids = list.stream().map(x -> x.getRoleId()).toArray(Long[]::new);
		if(role_ids.length == 0) {
			return ModelFactory.create(QueryResult.class);
		} else {
			return this.findByIds(role_ids);
		}
	}

	@MethodInfo("查询")
	@DaoQuery(entity=Role.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public Role findById(
			@Param(value=Role.ROLE_ID, required=true,	desc="角色ID") Long role_id
		) throws Exception {
		return null;
	}

}